home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / misc.swg / 0203_Random Dice roller.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-08-30  |  1.5 KB  |  78 lines

  1. {
  2. (* NOTE *)
  3. Dice Object was created by Todd A. Jacobs and is hereby released
  4. into the public domain.  Long live SWAG!
  5.  
  6. This unit is intended as a stand-alone object.  The idea was to
  7. create a reusable object for dice games, such that types for 2d6,
  8. 2d10, 1d20, etc. (you role-playing gamers know what I mean) wouldn't
  9. have to be created for each dice type.
  10.  
  11. The following sample code shows it's usage by writing a screenful of
  12. random dice rolls:
  13.  
  14.     program DiceDemo;
  15.  
  16.     uses Dice;
  17.  
  18.     var
  19.         d6: TDice;
  20.                  i: byte;
  21.  
  22.     begin
  23.         randomize;
  24.         d6.init (3, 6);
  25.         for i := 1 to 23 do
  26.                     writeln (d6.roll);
  27.         d6.done;
  28.         readln;
  29.     end. (*DiceDemo*)
  30.  
  31. No, it didn't have to be an object, but that's what I wanted to do.
  32. Use it any way you like. =)
  33.  
  34. If you have any improvements to offer, please submit them to SWAG.
  35. Thanks!
  36. }
  37.  
  38. Unit
  39.     Dice;
  40.  
  41. interface
  42.  
  43. type
  44.     TDice = object
  45.         NumDice: byte;
  46.         Sides: byte;
  47.         constructor Init (iDice, iSides: byte);
  48.         function Roll: word; virtual;
  49.         destructor Done; virtual;
  50.     end; {type definition of TDice}
  51.  
  52. implementation
  53.  
  54. constructor TDice.Init;
  55. begin
  56.      NumDice := iDice;
  57.      Sides := iSides;
  58. end;
  59.  
  60. function TDice.Roll;
  61. var
  62.     iLoopCounter: byte;
  63.     CurrValue: word;
  64. begin
  65.     CurrValue := 0;
  66.     while iLoopCounter < NumDice do begin
  67.         CurrValue := CurrValue + Random (Sides) + 1;
  68.         inc (iLoopCounter);
  69.         end; {while iLoopCounter}
  70.     Roll := CurrValue;
  71. end; {function Roll}
  72.  
  73. destructor TDice.Done;
  74. begin
  75. end;
  76.  
  77. end. {Unit Dice}
  78.